package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.pojo.Menu;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作
 */
@RestController
@RequestMapping("/user")
public class UserController {
    //获得当前登录用户的用户名
    @RequestMapping("/getUsername")
    public Result getUsername(){
        //当Spring security完成认证后，会将当前用户信息保存到框架提供的上下文对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println(user);
        if(user != null){
            String username = user.getUsername();
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,username);
        }

        return new Result(false, MessageConstant.GET_USERNAME_FAIL);
    }



    @Reference
    private UserService userService;

    //新增用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user , Integer[] roleIds){
        try{
            userService.add(user,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);//新增失败
        }
        return new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS);//新增成功
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return userService.pageQuery(queryPageBean);
    }

    //根据ID查询用户
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            User user = userService.findById(id);
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,user);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询失败
        }
    }

    //根据用户ID查询用户包含的多个角色ID
    @RequestMapping("/findroleIdsByuserId")
    public Result findroleIdsByuserId(Integer id){
        try{
            List<Integer> roleIds = userService.findroleIdsByuserId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,roleIds);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);//查询失败
        }
    }

    //编辑用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds){
        try{
            userService.edit(user,roleIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);//新增失败
        }
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);//新增成功
    }


    //删除检查项
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            userService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return  new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //查询所有用户
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckGroup> list = userService.findAll();
            return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);//查询失败
        }
    }

    @RequestMapping("/getRoleAndMenu")
    public Result getRoleAndMenu(){
        //从session中获取用户对象
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //如果User不为空
        if (user != null){
            String username = user.getUsername();//获取用户UserName
            Integer roleId = userService.findRoleByUserName(username);//根据UserName获得用户角色ID
            try {
                List<Menu> list = userService.getMenuByRoleId(roleId);//根据用户角色ID查询菜单数据
                return new Result(true,MessageConstant.GET_MENU_SUCCESS,list);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,MessageConstant.GET_MENU_FAIL);
            }
        }
        return new Result(false,MessageConstant.GET_MENU_FAIL);
    }
}
